package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "检查项模块管理")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;


      //   开发 检查项模块   crud
    @PostMapping("checkitem/add")
    @ApiOperation(value = "添加或修改检查项方法",notes = "添加或修改检查项信息")
    public Result add(@RequestBody CheckItem checkItem){
        Boolean b = checkItem.getId()==null;
        try {
            checkItemService.saveOrUpdate(checkItem);
            System.out.println(checkItem.getId());
            if(b){
                return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
            }else {
                return  new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if(b){
                return  new Result(true, MessageConstant.ADD_CHECKITEM_FAIL);
            }else {
                return  new Result(true, MessageConstant.UPDATE_CHECKITEM_FAIL);
            }
        }

    }

    @PostMapping("checkitem/findPage")
    @ApiOperation(value = "查询检查项方法",notes = "分页查询检查项信息")
    public  Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            Map map = checkItemService.findPageByContion(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @DeleteMapping("checkitem/deleteCheckItemById")
    @ApiOperation(value = "删除检查项方法",notes = "根据ID删除检查项信息")
    public Result deleteCheckItemById(@RequestParam("id") Integer id){
        try{
            CheckItem checkItem = new CheckItem();
            checkItem.setId(id);
            checkItem.setIs_delete(1);
            checkItemService.updateById(checkItem);
            return new Result(true,MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKITEM_FAIL);
        }
    }

    @GetMapping("checkitem/findAll")
    @ApiOperation(value = "查询检查项方法",notes = "查询所有的检查项列表信息")
    public Result findAll(){
        try {
            List<CheckItem> list = checkItemService.list();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

}
